package nz.ac.massey.jbuif.view;

import java.util.Comparator;

/**
 * ObjectSorter class
 * @author ahkiouma
 * @param <T> generic object type
 */
public class ObjectSorter<T> implements Comparator<T> {

	private boolean sortByName = true;

	
	/**
	 * compare teo objects
	 * @param object1
	 * @param object2
	 * @return integer result of comparision
	 * 
	 */
	public int compare(T o1, T o2) {
		if (o1 == null) {
			return -1;
		}
		if (o2 == null) {
			return 1;
		}

		// if sorting by name, or they are the same class, compare names
		if (sortByName || (!sortByName && o1.getClass().equals(o2.getClass()))) {
			return o1.toString().compareTo(o2.toString());
		} else // compare classes
		{
			return o1.getClass().toString().compareTo(o2.getClass().toString());
		}
	}

	
	/**
	 * sort the objects by name
	 * @param sortByName set the sort by name on/off
	 */
	public void setSortByName(boolean sortByName) {
		this.sortByName = sortByName;
	}

	
	/**
	 * get the sorted by name value
	 * @return get the sort by name value condition
	 */
	public boolean isSortByName() {
		return sortByName;
	}
}
